﻿using System;
using System.IO;
using System.Security.AccessControl;

namespace KPIS.GERP.Models.Class
{
	public static class FileUtility
	{
        public static void FolderACL(String accountName, String folderPath)
        {
            FileSystemRights Rights;

            //What rights are we setting?
            Rights = FileSystemRights.FullControl;
            bool modified;
            InheritanceFlags none = new InheritanceFlags();
            none = InheritanceFlags.None;

            //set on dir itself
            FileSystemAccessRule accessRule = new FileSystemAccessRule(accountName, Rights, none, PropagationFlags.NoPropagateInherit, AccessControlType.Allow);
            DirectoryInfo dInfo = new DirectoryInfo(folderPath);
            DirectorySecurity dSecurity = dInfo.GetAccessControl();
            dSecurity.ModifyAccessRule(AccessControlModification.Set, accessRule, out modified);

            //Always allow objects to inherit on a directory 
            InheritanceFlags iFlags = new InheritanceFlags();
            iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

            //Add Access rule for the inheritance
            FileSystemAccessRule accessRule2 = new FileSystemAccessRule(accountName, Rights, iFlags, PropagationFlags.InheritOnly, AccessControlType.Allow);
            dSecurity.ModifyAccessRule(AccessControlModification.Add, accessRule2, out modified);

            dInfo.SetAccessControl(dSecurity);
        }

		public static bool IsItVirtualpath(string filePath)
		{
			try
			{
				filePath = (filePath ?? string.Empty).Trim();
				return filePath.Substring(1, 1) == ":";
			} catch
			{
				return true;
			}
		}

		public static string SingleSlashAtEndString(string filePath)
		{
			filePath = (filePath ?? string.Empty).Trim();
			return !filePath.EndsWith("\\") && !filePath.EndsWith("/") ? filePath + "/" : filePath;
		}

		public static string GetMimeType(string fileName)
		{
			string mimeType = "application/unknown";
			var extension = Path.GetExtension(fileName);
			if (extension != null)
			{
				string ext = extension.ToLower();
				Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); // henter info fra windows registry
				if (regKey != null && regKey.GetValue("Content Type") != null)
				{
					mimeType = regKey.GetValue("Content Type").ToString();
				}
				else if (ext == ".png") // a couple of extra info, due to missing information on the server
				{
					mimeType = "image/png";
				}
				else if (ext == ".flv")
				{
					mimeType = "video/x-flv";
				}
			}
			return mimeType;
		}

        public static string GetFileSizeHumanReadable(double fileSize)
        {
            string[] sizes = { "B", "KB", "MB", "GB" };
            double len = fileSize;// new FileInfo(filename).Length;
            int order = 0;
            while (len >= 1024 && order + 1 < sizes.Length)
            {
                order++;
                len = len / 1024;
            }

            // Adjust the format string to your preferences. For example "{0:0.#}{1}" would
            // show a single decimal place, and no space.
            return string.Format("{0:0.##} {1}", len, sizes[order]);
        }
	}
}
